package com.augrain.microframework.lock.lock;

import com.augrain.microframework.lock.model.LockInfo;
import org.redisson.api.RedissonClient;

/**
 * 锁工厂
 *
 * @author biaoy
 * @since 2021/06/10
 */
public class LockFactory {

    private static RedissonClient redissonClient;

    public static Lock getLock(LockInfo lockInfo) {
        return switch (lockInfo.getType()) {
            case FAIR -> new FairLock(redissonClient, lockInfo);
            case READ -> new ReadLock(redissonClient, lockInfo);
            case WRITE -> new WriteLock(redissonClient, lockInfo);
            default -> new ReentrantLock(redissonClient, lockInfo);
        };
    }

    public static void setRedissonClient(RedissonClient redissonClient) {
        LockFactory.redissonClient = redissonClient;
    }
}
